package com.esiag.isidis.fi.pdsteam.content;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import com.esiag.isidis.fi.pdsteam.model.Event;
import com.esiag.isidis.fi.pdsteam.model.EventInfo;
import com.esiag.isidis.fi.pdsteam.model.SensorType;
import com.esiag.isidis.fi.pdsteam.random.Randomizer;
import com.esiag.pdsteam.referencement.bean.Message;
import com.esiag.pdsteam.referencement.bean.MessageSource;
import com.esiag.pdsteam.referencement.bean.Source;
import com.esiag.pdsteam.referencement.dao.DataBaseAccess;

public class EventFinderFromDB implements EventFinder {


	public EventFinderFromDB() {
		super();

	}

	public ArrayList<Event> getEventsByCriticity(EventInfo info) {
		ArrayList<Event> events=new ArrayList<Event>();
		DataBaseAccess databaseAccess = new DataBaseAccess();
		Set<Source> sensorsDB = new HashSet<Source>();
		try {
			if( info.getType() != SensorType.SIMPLE)
				sensorsDB = databaseAccess.getAllSourcebyType(1+info.getType().ordinal());
			else
				sensorsDB = databaseAccess.getAllSource();
			ArrayList<Source> sensorsRand = Randomizer.rand( info.getEventCount(), sensorsDB); 
			for(Source sensor: sensorsRand) {
				Event event=new Event();
				event.setWaitTime(info.getWaitTime());
				event.setSensorMacAddress( sensor.getAdrMac());
				Set<Message> messages = databaseAccess.getAllMessagebySource(sensor.getId());
				Message messagesRand = Randomizer.rand(messages);
				event.setEventCode( messagesRand.getCode());
				events.add(event);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return events;
	}

	public ArrayList<Event> getALL() {
		// TODO Auto-generated method stub
		return null;
	}

}
